<template>
  <div class="modal-overlay" @click.self="closeModal">
    <div class="modal">
      <div class="modal-header">
        <h2>Add New Movie</h2>
        <button class="close-btn" @click="closeModal">×</button>
      </div>
      
      <form @submit.prevent="handleSubmit">
        <div class="form-group">
          <label for="title">Title</label>
          <input
            type="text"
            id="title"
            v-model="formData.title"
            required
            placeholder="Enter movie title"
          />
          <p class="error" v-if="errors.title">{{ errors.title }}</p>
        </div>
        
        <div class="form-group">
          <label for="year">Year</label>
          <input
            type="number"
            id="year"
            v-model.number="formData.year"
            required
            placeholder="Enter release year"
            min="1800"
            max="2100"
          />
          <p class="error" v-if="errors.year">{{ errors.year }}</p>
        </div>
        
        <div class="form-group">
          <label for="genre">Genre</label>
          <select id="genre" v-model="formData.genre" required>
            <option value="">Select a genre</option>
            <option>Action</option>
            <option>Adventure</option>
            <option>Comedy</option>
            <option>Drama</option>
            <option>Fantasy</option>
            <option>Horror</option>
            <option>Sci-Fi</option>
            <option>Thriller</option>
          </select>
          <p class="error" v-if="errors.genre">{{ errors.genre }}</p>
        </div>
        
        <div class="form-group">
          <label for="rating">Rating</label>
          <input
            type="number"
            id="rating"
            v-model.number="formData.rating"
            required
            placeholder="Enter movie rating (0-10)"
            min="0"
            max="10"
            step="0.1"
          />
          <p class="error" v-if="errors.rating">{{ errors.rating }}</p>
        </div>
        
        <div class="form-group">
          <label for="poster">Poster URL</label>
          <input
            type="url"
            id="poster"
            v-model="formData.poster"
            placeholder="Enter poster image URL"
          />
          <p class="error" v-if="errors.poster">{{ errors.poster }}</p>
        </div>
        
        <div class="form-group">
          <label for="description">Description</label>
          <textarea
            id="description"
            v-model="formData.description"
            required
            placeholder="Enter movie description"
            rows="4"
          ></textarea>
          <p class="error" v-if="errors.description">{{ errors.description }}</p>
        </div>
        
        <div class="form-actions">
          <button type="submit" class="btn btn-primary">Add Movie</button>
        </div>
      </form>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import apiService from '@/services/api';

const emit = defineEmits(['close', 'movie-added']);

const formData = ref({
  title: '',
  year: '',
  genre: '',
  rating: '',
  poster: '',
  description: ''
});

const errors = ref({});
const isLoading = ref(false);

const closeModal = () => {
  emit('close');
};

const handleSubmit = async () => {
  errors.value = {};
  
  // 验证标题
  if (!formData.value.title) {
    errors.value.title = 'Title is required';
  }
  
  // 验证年份
  if (!formData.value.year) {
    errors.value.year = 'Year is required';
  } else if (formData.value.year < 1800 || formData.value.year > 2100) {
    errors.value.year = 'Please enter a valid year (1800-2100)';
  }
  
  // 验证类型
  if (!formData.value.genre) {
    errors.value.genre = 'Genre is required';
  }
  
  // 验证评分
  if (!formData.value.rating) {
    errors.value.rating = 'Rating is required';
  } else if (formData.value.rating < 0 || formData.value.rating > 10) {
    errors.value.rating = 'Rating must be between 0 and 10';
  }
  
  // 验证描述
  if (!formData.value.description) {
    errors.value.description = 'Description is required';
  }
  
  // 如果有错误，不提交
  if (Object.keys(errors.value).length > 0) {
    return;
  }
  
  isLoading.value = true;
  
  try {
    const movieData = {
      title: formData.value.title,
      year: formData.value.year,
      genre: formData.value.genre,
      rating: formData.value.rating,
      poster: formData.value.poster || null,
      description: formData.value.description
    };
    
    const response = await apiService.addMovie(movieData);
    
    // 如果添加成功，清空表单并关闭模态框
    formData.value = {
      title: '',
      year: '',
      genre: '',
      rating: '',
      poster: '',
      description: ''
    };
    
    emit('movie-added');
    closeModal();
  } catch (error) {
    console.error('Failed to add movie:', error);
    errors.value.general = 'Failed to add movie. Please try again later.';
  } finally {
    isLoading.value = false;
  }
};
</script>

<style scoped>
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
}

.modal {
  background-color: white;
  border-radius: 8px;
  width: 100%;
  max-width: 500px;
  padding: 25px;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 25px;
}

.close-btn {
  background: none;
  border: none;
  font-size: 24px;
  cursor: pointer;
  color: #666;
}

.form-group {
  margin-bottom: 20px;
}

.form-group label {
  display: block;
  margin-bottom: 8px;
  font-weight: 500;
  color: #333;
}

.form-group input,
.form-group select,
.form-group textarea {
  width: 100%;
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 16px;
}

.form-group .error {
  color: #e94560;
  font-size: 14px;
  margin-top: 5px;
}

.form-actions {
  margin-top: 25px;
}

.btn {
  padding: 12px 20px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-weight: 500;
  width: 100%;
}

.btn-primary {
  background-color: #e94560;
  color: white;
}

.btn:hover {
  opacity: 0.9;
}

textarea {
  resize: vertical;
}
</style>